package com.wang.study.controller;

import com.alibaba.fastjson.JSONObject;
import com.wang.study.Proxy.JdkDynamicProxyUtil;
import com.wang.study.commons.BaseServlet;
import com.wang.study.model.Cart;
import com.wang.study.model.CartAndUidAndCategory;
import com.wang.study.service.CartService;
import com.wang.study.service.impl.CartServiceImp;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/cart.do")
public class CartController extends BaseServlet {
    private CartService cartService =  new JdkDynamicProxyUtil<CartService>().getProxyObject(new CartServiceImp());
    /**
     * 添加到购物车
     * @param req
     * @param resp
     */
    public void addCart(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String uid = req.getParameter("uid");
        String gid = req.getParameter("gid");
        String num = req.getParameter("num");
        String subtotal = req.getParameter("subtotal");

        Cart cart = new Cart(Integer.valueOf(uid),Integer.valueOf(gid), Integer.valueOf(num), Double.valueOf(subtotal));
        Integer i = cartService.insertCart(cart);
        Boolean flag = i>0?true:false;
        resp.getWriter().write(new JSONObject().toJSONString(flag));
    }

    /**
     * 通过uid获取购物车信息
     * @param req
     * @param resp
     */
    public void selectCartByUid(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String uid = req.getParameter("uid");
        System.out.println("====================");
        System.out.println(uid);
        ArrayList<CartAndUidAndCategory> cs = new ArrayList<>();
        //id name image nums total
        List<Cart> cart= cartService.selectCartByUid(Integer.valueOf(uid));
        if(null!=cart){
            for (int i = 0; i < cart.size(); i++) {
                Integer goodid = cart.get(i).getGoodid();
                CartAndUidAndCategory cartAll = cartService.selectCart(goodid);
                cs.add(cartAll);
            }
            resp.getWriter().write(new JSONObject().toJSONString(cs));
        }else{
            resp.getWriter().write(new JSONObject().toJSONString(false));
        }
    }

    /**
     * 通过id删除购物车信息
     * @param req
     * @param resp
     */
    public void delCartById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        Integer i= cartService.delCartById(Integer.valueOf(id));
        if(i>0){
            resp.getWriter().write(new JSONObject().toJSONString(Boolean.valueOf(true)));
        }else{
            resp.getWriter().write(new JSONObject().toJSONString(Boolean.valueOf(false)));
        }
    }

    /**
     * 修改数据库
     * @param req
     * @param resp
     */
    public void upDateById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String num = req.getParameter("num");
        String total = req.getParameter("total");
        Integer i= cartService.upDateById(Integer.valueOf(id),Integer.valueOf(num),Integer.valueOf(total));
        if(i>0){
            resp.getWriter().write(new JSONObject().toJSONString(Boolean.valueOf(true)));
        }else{
            resp.getWriter().write(new JSONObject().toJSONString(Boolean.valueOf(false)));
        }
    }
}
